using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommerceOne.Domains.IRepositories;
using CommerceOne.Models.Entities;
namespace CommerceOne.Domains.SqlRepositories
{
    public class SqlProductCategoryRepository : SqlRepositoryBase, IProductCategoryRepository
    {
        /// <summary>
        /// Loads all category.
        /// </summary>
        /// <param name="takeParentOnly">if set to <c>true</c> [take parent only].</param>
        /// <returns></returns>
        public List<ProductCategory> LoadAllCategory(bool  takeParentOnly)
        {
            if (takeParentOnly)
            {
                return this.SqlContext.ProductCategories.Where(p=>p.ParentID == null).ToList();
            }

            List<ProductCategory> list = this.SqlContext.ProductCategories.ToList();
            return list;
        }

        /// <summary>
        /// Gets the category with id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public ProductCategory GetCategoryWithId(int id)
        {
            return this.SqlContext.ProductCategories.FirstOrDefault(p => p.ProductCategoryID == id);
        }

        #region Singleton Instance
        private static SqlProductCategoryRepository _instance;
        public static SqlProductCategoryRepository Instance
        {
            get
            {
                if (_instance == null) _instance = new SqlProductCategoryRepository();
                return _instance;
            }
        }
        #endregion
    }
} 
